package org.jeecg.modules.flowerAdjust.service.impl;

import org.jeecg.modules.flowerAdjust.entity.TFlowerAdjustTagInMain;
import org.jeecg.modules.flowerAdjust.entity.TFlowerAdjustTagIn;
import org.jeecg.modules.flowerAdjust.mapper.TFlowerAdjustTagInMapper;
import org.jeecg.modules.flowerAdjust.mapper.TFlowerAdjustTagInMainMapper;
import org.jeecg.modules.flowerAdjust.service.ITFlowerAdjustTagInMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 批量入库调整主表
 * @Author: jeecg-boot
 * @Date:   2022-11-16
 * @Version: V1.0
 */
@Service
public class TFlowerAdjustTagInMainServiceImpl extends ServiceImpl<TFlowerAdjustTagInMainMapper, TFlowerAdjustTagInMain> implements ITFlowerAdjustTagInMainService {

	@Autowired
	private TFlowerAdjustTagInMainMapper tFlowerAdjustTagInMainMapper;
	@Autowired
	private TFlowerAdjustTagInMapper tFlowerAdjustTagInMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TFlowerAdjustTagInMain tFlowerAdjustTagInMain, List<TFlowerAdjustTagIn> tFlowerAdjustTagInList) {
		tFlowerAdjustTagInMainMapper.insert(tFlowerAdjustTagInMain);
		for(TFlowerAdjustTagIn entity:tFlowerAdjustTagInList) {
			//外键设置
			entity.setMainid(tFlowerAdjustTagInMain.getId());
			tFlowerAdjustTagInMapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TFlowerAdjustTagInMain tFlowerAdjustTagInMain,List<TFlowerAdjustTagIn> tFlowerAdjustTagInList) {
		tFlowerAdjustTagInMainMapper.updateById(tFlowerAdjustTagInMain);
		
		//1.先删除子表数据
		tFlowerAdjustTagInMapper.deleteByMainId(tFlowerAdjustTagInMain.getId());
		
		//2.子表数据重新插入
		for(TFlowerAdjustTagIn entity:tFlowerAdjustTagInList) {
			//外键设置
			entity.setMainid(tFlowerAdjustTagInMain.getId());
			tFlowerAdjustTagInMapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tFlowerAdjustTagInMapper.deleteByMainId(id);
		tFlowerAdjustTagInMainMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tFlowerAdjustTagInMapper.deleteByMainId(id.toString());
			tFlowerAdjustTagInMainMapper.deleteById(id);
		}
	}

	@Override
	public int updateTagInfo(int tagNum, String varietyCode) {
		int i=tFlowerAdjustTagInMainMapper.updateTagInfo(tagNum,varietyCode);
		return i;
	}

}
